package br.inf.solutions.apresentacoes.ssp.view.composer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Combobox;

import br.com.vexillum.util.ReflectionUtils;
import br.com.vexillum.util.Return;
import br.com.vexillum.util.SpringFactory;
import br.inf.solutions.apresentacoes.ssp.control.PromotionController;
import br.inf.solutions.apresentacoes.ssp.control.SquareController;
import br.inf.solutions.apresentacoes.ssp.control.SquarePostGraduationController;
import br.inf.solutions.apresentacoes.ssp.control.basics.CriterionPromotionController;
import br.inf.solutions.apresentacoes.ssp.model.Person;
import br.inf.solutions.apresentacoes.ssp.model.Square;
import br.inf.solutions.apresentacoes.ssp.model.SquarePostGraduation;
import br.inf.solutions.apresentacoes.ssp.model.basics.CriterionPromotion;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.items.Promotion;

@SuppressWarnings("serial")
@Service
@Scope("prototype")
public class PromotionComposer extends BulletinItemComposer<Promotion, PromotionController> {

	private Square searchSquare;
	
	public Square getSearchSquare() {
		return searchSquare;
	}

	public void setSearchSquare(Square searchSquare) {
		this.searchSquare = searchSquare;
	}
	
	@Override
	protected String getUpdatePage() {
		return  "/paginas/promocao/modalCadastrar.zul";
	}

	@Override
	protected String getDeactivationMessage() {
		return null;
	}
	
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		loadBinder();
	}

	@Override
	public PromotionController getControl() {
		return SpringFactory.getController("promotionController",
				PromotionController.class,
				ReflectionUtils.prepareDataForPersistence(this));
	}

	@Override
	public Promotion getEntityObject() {
		return new Promotion();
	}

	@SuppressWarnings("unchecked")
	public List<CriterionPromotion> getCriterions() {
		CriterionPromotionController controlador = SpringFactory.getController(
				"criterionPromotionController", CriterionPromotionController.class,
				ReflectionUtils.prepareDataForPersistence(this));
		return (List<CriterionPromotion>) controlador.doAction("listAll").getList();
	}
	
	@SuppressWarnings("unchecked")
	public List<Square> getSquares() {
		SquareController controlador = SpringFactory.getController("squareController", SquareController.class,	ReflectionUtils.prepareDataForPersistence(this));
		return (List<Square>) controlador.doAction("listAll").getList();
	}

	@SuppressWarnings("unchecked")
	public List<SquarePostGraduation> getSquarePosts() {
		if (getEntity().getPerson() == null || getEntity().getPerson().getSquare() == null || getEntity().getPerson().getSquare().getSquarePostAssociations() == null) {
			return new ArrayList<SquarePostGraduation>();
		} else {
			HashMap<String, Object> data = new HashMap<>();
			SquarePostGraduationController controller = SpringFactory.getController("squarePostGraduationController", SquarePostGraduationController.class,	data);
			controller.refresh(getEntity().getPerson().getSquare());
			data.put("listEntity", getEntity().getPerson().getSquare().getSquarePostAssociations());
			data.put("person", getEntity().getPerson());
			return (List<SquarePostGraduation>) controller.doAction("getNextPostGraduation").getList();
		}

	}
	
	@SuppressWarnings("rawtypes")
	public EventListener getEventPersonNotFound(){
		return new EventListener<Event>() {

			@Override
			public void onEvent(Event event) throws Exception {
				if (event.getName().equals("onOK")) {
					Executions.sendRedirect("/paginas/pessoas/cadastrar.zul");
				} else {
					getEntity().setPerson(new Person());
					loadBinder();
				}
			}
		};
	}
	
	public void searchNotPublicatedPromotion(){
		Return ret = getControl().doAction("getNotPublicatedPromotion");
		if(ret.getList() != null && !ret.getList().isEmpty()){
			setEntity((Promotion) ret.getList().get(0));
			setEntity((Promotion) getEntity().cloneEntity());
		} else {
			Person person = getEntity().getPerson();
			setEntity(getEntityObject());
			getEntity().setPerson(person);
		}
		loadBinder();
	}
	
	@Override
	protected void clearForm() {
		super.clearForm();
		setSearchSquare(null);
		((Combobox) getComponentById("fldPost")).setRawValue(null);
	}
}
